package com.example.springboot_demo.service;

import com.example.springboot_demo.pojo.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 江岸
 * @version V1.0
 * @ClassName: mongoUserService
 * @description: TODO
 * @date 2020/8/511:36
 * @since V1.0
 */
@Service
public class MongoTestService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private static Logger log =LoggerFactory.getLogger(MongoTestService.class);



    //查询
    public List<ContrastBean> findAll(){
       // List<MarketContrastProduct> all = mongoTestMapper.findAll();
        Query query = new Query();
        Sort sort = Sort.by(Sort.Direction.DESC, "createTime");
        PageRequest of = PageRequest.of(0, 3500, sort);
        query.with(of);
        List<MarketContrastProduct> marketContrastProducts = mongoTemplate.find(query, MarketContrastProduct.class);
        ArrayList<ContrastBean> objects = new ArrayList<>();
        //log.info("查询所有结果:"+marketContrastProducts);
        for (MarketContrastProduct p:marketContrastProducts){
            ContrastBean contrastBean = new ContrastBean();
            contrastBean.setProductId(p.getProductId());
            contrastBean.setProductName(p.getProductName());
//            //查询保险责任
//            Query query2 = new Query(Criteria.where("productId").is(p.getProductId()).and("productState").ne(2));
//            List<MarketProductLiability> liabList = mongoTemplate.find(query2, MarketProductLiability.class);
//            Map<String, MarketProductLiability> map = liabList.parallelStream().collect(Collectors.toMap(MarketProductLiability::getProductLiabId,MarketProductLiability::getThis));
//            System.out.println(map);
            //查询模板中的责任
            MarketContrastTemplate template = mongoTemplate.findById(p.getTemplateId(), MarketContrastTemplate.class);
            if(template.getLiabilityItems()!=null && !template.getLiabilityItems().isEmpty()) {
                template.setLiabilityItems(getFlatItems(template.getLiabilityItems()));
            }
            contrastBean.setTemplateName(template.getTemplateName());
            contrastBean.setIsDesc(template.getIsDesc());
            //查询产品对比项集合
            Query query3 = new Query(Criteria.where("constrastProductId").is(p.getConstrastProductId()).and("productId").is(p.getProductId()).and("itemType").is("liability").and("productType").is("M"));
            List<MarketContrastProductItem> contrastProItems = mongoTemplate.find(query3, MarketContrastProductItem.class);
            //匹配责任和对比项目
            ArrayList<ItemBean> list = new ArrayList<>();
            for (MarketContrastTemplateItem m:template.getLiabilityItems()){
                ItemBean itemBean = new ItemBean();
                itemBean.setItemId(m.getItemId());
                itemBean.setItemName(m.getItemName());
                for (MarketContrastProductItem item:contrastProItems){
                    if (m.getItemId().equals(item.getItemId())){
 //                       itemBean.setLibName(map.get(item.getLibId())==null?null:map.get(item.getLibId()).getProductLiabName());

                        itemBean.setSimpleDesc(item.getSimpleDesc());
                        itemBean.setFullDesc(item.getFullDesc());
                        itemBean.setRemark(item.getRemark());
                        if (EmptyUtils.isEmpty(itemBean.getSimpleDesc())&&EmptyUtils.isNotEmpty(itemBean.getFullDesc())){
                            itemBean.setSimpleDesc(itemBean.getFullDesc());
                        }
                        if (EmptyUtils.isEmpty(item.getFullDesc())&&EmptyUtils.isNotEmpty(itemBean.getSimpleDesc())){
                            itemBean.setFullDesc(itemBean.getSimpleDesc());
                        }
//                        if (item.getSimpleDesc()!=null){
//                            itemBean.setSimpleDesc(FilterHtmlUtils.html2Text(item.getSimpleDesc()));
//                        }
//                        if (item.getFullDesc() !=null){
//                            itemBean.setFullDesc(FilterHtmlUtils.html2Text(item.getFullDesc()));
//                        }
//                        if (item.getRemark()!=null){
//                            itemBean.setRemark(FilterHtmlUtils.html2Text(item.getRemark()));
//                        }
                    }
                }
                list.add(itemBean);
            }
            contrastBean.setItemBeans(list);
            mongoTemplate.save(contrastBean);
            objects.add(contrastBean);
        }
        return objects;
    }



    /**
     *
     * @Description 将层级Item集合转成平铺item集合
     * @since V1.2.1
     * @param treeItems
     * @return
     */
    private List<MarketContrastTemplateItem> getFlatItems(List<MarketContrastTemplateItem> treeItems) {
        List<MarketContrastTemplateItem> flatItems = new ArrayList<MarketContrastTemplateItem>();

        //平铺第一级
        for(MarketContrastTemplateItem item : treeItems) {
            List<MarketContrastTemplateItem> leafItems = item.itemChildren;
            item.setItemChildren(null);
            flatItems.add(item);
            //平铺第二级
            if(leafItems!=null && !leafItems.isEmpty()) {
                for(MarketContrastTemplateItem leafItem : leafItems) {
                    leafItem.itemName = item.itemName + "-" + leafItem.itemName;
                    flatItems.add(leafItem);
                }
            }
        }
        return flatItems;
    }

    public  void  medicalCondition(){
        Query query = new Query();
        query.fields().include("condition");
        List<MedicalProduct> medicalProducts = mongoTemplate.find(query, MedicalProduct.class);
        for (MedicalProduct item:medicalProducts){
            item.getCondition().setProductId(item.getProductId());
            mongoTemplate.save(item.getCondition());
        }
    }
}
